const fs=require('fs');
const path=require('path');
const http=require('http');
var list=[];
function createFileTime(time){
    var date=new Date(time);
    var year=date.getFullYear();
    var month=date.getMonth()+1;
    var day=date.getDate();
    var hour=date.getHours();
    var minute=date.getMinutes();
    return `${year}-${month}-${day} ${hour}:${minute}`;
}
http.createServer((req,res)=>{
    var htmlPath=path.join(__dirname,'/index.html');
    var imgPath=path.join(__dirname,'/1.png');
    var urlStr=req.url;
    if(urlStr=='/'){
        var htmlContent=fs.readFileSync(htmlPath);
        res.writeHead(200,{'content-type':'text/html;charset=utf-8'});
        res.write(htmlContent);
        res.end();
        
    }else if(urlStr=='/1.png'){
        var imgContent=fs.readFileSync(imgPath);
        res.writeHead(200,{'content-type':'image/png'})  //返回给浏览器的是buffer数据，content-type就是告诉浏览器返回的数据按什么格式解析
        res.write(imgContent);
        res.end();
    }else if(urlStr=='/getlist'){
        var dirPath=path.join(__dirname,'/filedir');
        var files=fs.readdirSync(dirPath);
        for(var i=0;i<files.length;i++){
            var filePath=path.join(dirPath,'/'+files[i])
            var fileObj=fs.statSync(filePath);
            if(fileObj.isFile()){
                var fileType='file';
                var index=files[i].indexOf('.');
                var fileName=files[i].slice(0,index);
            }else{
                var fileType="folder";
                var fileName=files[i];
            }
            list[i]={
                fileType:fileType,
                fileName:fileName,
                fileSize:fileObj.size+'byte',
                fileTime:createFileTime(fileObj.ctimeMs),
                fileTemp:fileObj.ctimeMs,
                totalName:files[i]
            }
        }
        var listStr=JSON.stringify(list);
        res.writeHead(200,{'content-type':'text/plain'});
        res.write(listStr);
        res.end();
    }else if(urlStr.indexOf('/del?delId=')==0){
        var usplit=urlStr.split('=');
        var queryStr=usplit[1];
        var targetEle=queryStr.slice(3,);
        for(var j=0;j<list.length;j++){
            if(list[j].fileTemp==targetEle){
                list.splice(j,1);
                res.end('ok');
            }
        }
    }else if(urlStr.indexOf('/getfile?fileId=')==0){
        var usplit=urlStr.split('=');
        var queryStr=usplit[1];
        var targetEle=queryStr.slice(4,);
        for(var i=0;i<list.length;i++){
            if(list[i].fileTemp==targetEle){
                var dirPath=path.join(__dirname,'/filedir');
                var filePath=path.join(dirPath,'/'+list[i].totalName);
                if(list[i].fileType=='file'){
                    var content=fs.readFileSync(filePath);
                    res.writeHead(200,{'content-type':'text/plain;charset=utf-8'});
                    res.write(content);
                    res.end();
                }else{
                    var childlist=[];
                    var files=fs.readdirSync(filePath);
                    for(var i=0;i<files.length;i++){
                        var filePath=path.join(dirPath,'/'+files[i])
                        var fileObj=fs.statSync(filePath);
                        if(fileObj.isFile()){
                            var fileType='file';
                            var index=files[i].indexOf('.');
                            var fileName=files[i].slice(0,index);
                        }else{
                            var fileType="folder";
                            var fileName=files[i];
                        }
                        childlist[i]={
                            fileType:fileType,
                            fileName:fileName,
                            fileSize:fileObj.size+'byte',
                            fileTime:createFileTime(fileObj.ctimeMs),
                        }
                    }
                    var listStr=JSON.stringify(childlist);
                    res.writeHead(200,{'content-type':'text/plain'});
                    res.write(listStr);
                    res.end();
                }
            }
        }
    }
}).listen(8081);

// http://domain:8081/upload?title='hhh'        /upload?title='hhh'是req
// 协议    域名   端口 资源路径  参数
//response可以返回的数据类型:1.web page    2.css    3.js    4.image   5.file   6.json data